#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <set>

using namespace std;

    vector<string> fullJustify(vector<string> &words, int L) {
	vector<string> ans;
	
	for (int i = 0; i < words.size(); ) {
	    int size = 0;
	    vector<string> vec;
	    while(i < words.size() && size  + vec.size() + words[i].size() <= L) {
		vec.push_back(words[i]);
		size += words[i].size();
		i++;
	    }	  
	    int space, first;
	    if (vec.size() != 1) {
		space = (L - size)/(vec.size()-1); 
		first = (L-size)%(vec.size()-1) + space; 
	    } else {
		first = L - size;
	    }

	    string str;
//	    cout << L << " " << size << " " << vec.size() << endl;
//	    cout << space << " " << first << endl;
	    for (int k = 0; k < vec.size(); k++) {
		str += vec[k];
		if (k == 0) str += string(first, ' ');
		else if (k != vec.size()-1) str += string(space, ' ');
	    }	
	    cout << str << endl;	
	    ans.push_back(str);
	}
	return ans;
    }
//  ["What","must","be","shall","be."], 12  ["What must be","shall    be."] ["What must be","shall be.   "] 
  
int main(int argc, char **argv)
{
    string str[] = {"This", "is", "an", "example", "of", "text", "justification."};
    vector<string> words;
    for (int i = 0; i < 7; i++) 
	words.push_back(str[i]);
    int L = 16;
    fullJustify(words, L);

    cout << "-----------------Test 1--------------------" << endl;


    cout << "-----------------Test 2--------------------" << endl;


    cout << "-----------------Test 3--------------------" << endl;


    cout << "-----------------Test 4--------------------" << endl;


    cout << "-----------------Test 5--------------------" << endl;



}
